import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col} from 'antd';
import {DATE_FORMAT} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import {PERMISSION} from './constants';
import {hasPermissions} from './utils';
import routes from './routes';
import {injectIntl} from './intl';
import {hobby, gender, customerType, salesLeadIntentLevel, potentialCustomerMainDataSource, salesLeadInformationSource} from '../Enum';
import {conventEnumValueToString, formatDateTime} from '../utils';
import styles from './style.css';
import {getDetail, abandonOrder} from './api';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
const {Description} = DescriptionList;
export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
        data: {},
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({loading: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                    });
                else
                    this.setState(({
                        loading: false
                    }));
            });
    }
    handleUpdateClick = () => {
        this.props.history.push(routes.update.format(this.props.id));
    }
    onClickAbandonBtn = () => {
        this.setState({loading: true});
        abandonOrder(this.state.data.id, this.state.data.rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {data} = this.state;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.abandon) && hasPermissions(data.options, PERMISSION.abandon))
            operations.push(<WrappedPopconfirm
                key="abandon"
                id={this.props.id}
                placement="topLeft"
                onConfirm={this.onClickAbandonBtn}
                title={formatMessage({
                    id: 'detailPanel.validate.abandon',
                    defaultMessage: '是否确认作废？'
                })}>
                <Button key="abandon" size="small">{
                    formatMessage({
                        id: 'detailPanel.btn.abandon',
                        defaultMessage: '作废'
                    })
                }</Button>
            </WrappedPopconfirm>);
        return (
            <div className="detail">
                <Card loading={this.state.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'detailPanel.text.title',
                        defaultMessage: '潜客信息详情'
                    })}</Col>
                    <Col className={`col-align-right ${styles.detail}`} span={18}>{operations}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.name',
                            defaultMessage: '客户姓名'
                        })}>{data.name}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.gender',
                            defaultMessage: '性别'
                        })}>{conventEnumValueToString(gender, data.gender)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.birthday',
                            defaultMessage: '生日'
                        })}>{formatDateTime(data.birthday, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.cellPhoneNumber',
                            defaultMessage: '客户电话'
                        })}>{data.cellPhoneNumber}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.address',
                            defaultMessage: '地址'
                        })}>{data.address}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.email',
                            defaultMessage: 'Email'
                        })}>{data.email}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.type',
                            defaultMessage: '客户类型'
                        })}>{conventEnumValueToString(customerType, data.type)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.code',
                            defaultMessage: '潜客编号'
                        })}>{data.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandName',
                            defaultMessage: '品牌'
                        })}>{data.brandName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerCode',
                            defaultMessage: '总代'
                        })}>{data.dealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.intentLevel',
                            defaultMessage: '意向级别'
                        })}>{conventEnumValueToString(salesLeadIntentLevel, data.intentLevel)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.productCategoryCode',
                            defaultMessage: '意向车型'
                        })}>{data.productCategoryCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.intentionalVehicle',
                            defaultMessage: '意向车辆'
                        })}>{data.intentionalVehicle}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.hobby',
                            defaultMessage: '兴趣爱好'
                        })}>{conventEnumValueToString(hobby, data.hobby)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.mainDataSource',
                            defaultMessage: '主要来源'
                        })}>{conventEnumValueToString(potentialCustomerMainDataSource, data.mainDataSource)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.subdivisionDataSource',
                            defaultMessage: '细分来源'
                        })}>{conventEnumValueToString(salesLeadInformationSource, data.subdivisionDataSource)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.personality',
                            defaultMessage: '客户性格特征'
                        })}>{data.personality}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.otherBrand',
                            defaultMessage: '旧车品牌'
                        })}>{data.otherBrand}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.otherModel',
                            defaultMessage: '旧车车型'
                        })}>{data.otherModel}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.isTestDrive',
                            defaultMessage: '是否试乘试驾'
                        })}>{data.isTestDrive ? formatMessage({
                                id: 'detailPanel.text.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPanel.text.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.isQuotation',
                            defaultMessage: '是否报价'
                        })}>{data.isQuotation ? formatMessage({
                                id: 'detailPanel.text.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPanel.text.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.otherPreferredVehicleModel',
                            defaultMessage: '其他意向车型'
                        })}>{data.otherPreferredVehicleModel}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.salesLeadCode',
                            defaultMessage: '线索编号'
                        })}>{data.salesLeadCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.remark',
                            defaultMessage: '备注'
                        })}>{data.remark}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.creator',
                            defaultMessage: '创建人姓名'
                        })}>{data.creatorName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.createTime',
                            defaultMessage: '创建人时间'
                        })}>{formatDateTime(data.createTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.modifierName',
                            defaultMessage: '修改人姓名'
                        })}>{data.modifierName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.modifyTime',
                            defaultMessage: '修改时间'
                        })}>{formatDateTime(data.modifyTime, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.abandonerName',
                            defaultMessage: '作废人姓名'
                        })}>{data.abandonName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.abandonTime',
                            defaultMessage: '作废时间'
                        })}>{formatDateTime(data.abandonTime, DATE_FORMAT)}</Description>
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    id: PropTypes.string.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);


const mapStateToProps = (state, props) => ({
    id: props.id,
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    handleReceiveClick: () => {
        props.history.push(routes.receive.format(props.id));
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
